module Manager {
	
	export class ProtocolBuffer {

        private static _inst: ProtocolBuffer = null;

		private ProtoBuf:any = Laya.Browser.window.dcodeIO.ProtoBuf;
        private Packet:any;
		
		constructor() {
			if (ProtocolBuffer._inst !== null) {
                throw new Error("ProtocolBuffer Class Is An Instance Class");
            }
		}

        public static getInstance() {
            if (ProtocolBuffer._inst === null) {
                ProtocolBuffer._inst = new ProtocolBuffer();
                ProtocolBuffer._inst.init();
            }
            return ProtocolBuffer._inst;
        }

        public init(): void {
            Laya.loader.load("res/protos/Message.proto", Laya.Handler.create(this, this.onAssetsLoaded));
        }
		
		private onAssetsLoaded(data:string):void {
			this.Packet = this.ProtoBuf.loadProto(data).build().Message;
		}

        public encodeData(message:any) {
			var buffer:any = this.Packet.encode(message).toBuffer();
            return buffer;
        }

        public decodeData(buffer: any) {
            return this.Packet.decode(buffer);
        }

	}
}

// Usage:
// let pb = ProtocolBuffer.getInstance();
// setTimeout(function(){
//     let buffer = pb.encodeData({
//         "packetId":101,
//         "timestamp":12423545,
//         "msgHeadr":{
//             "msgType": 12,
//             "msgSize": 5
//         }});
//     let message = pb.decodeData(buffer);
// }, 5000);


